<?php
/**
 * Merge different css or js files together.
 *
 * @author @@AUTHOR@@
 * @copyright @@AUTHOR@@ @@YEAR@@
 * @version @@VERSION@@
 * @package Extensions
 * @subpackage Low-Cal_Vanilla
 * @license  @@LICENCE_URL@@ @@LICENCE@@
 *
 * $Id: packer.php 337 2008-11-03 10:44:38Z dinoboff $
 * $HeadURL: http://vanilla-friends.googlecode.com/svn/trunk/LowCalVanilla/packer.php $
 */

define('LOWCALVANILLA_DIR', dirname(__FILE__));
include(LOWCALVANILLA_DIR.'/../../appg/settings.php');
include(LOWCALVANILLA_DIR.'/../../appg/init_ajax.php');
include_once(LOWCALVANILLA_DIR.'/library/Functions.php');
include_once(LOWCALVANILLA_DIR.'/library/Class.Response.php');

/**
 * @global Context $Context
 * @global array $Configuration
 */
global $Context, $Configuration;

// Get last modification of the browser cached requested file
$ETag = 0;
$MathTime = 0;
if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) {
	$ETag = ForceInt($_SERVER['HTTP_IF_NONE_MATCH'], 0);
} elseif (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
	$Time = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
	if ($Time) {
		$MathTime = ForceInt($Time, 0);
	}
}

//Set response object
$Response = new LowCalVanilla_Response();
$Files = LowCalVanilla_GetFilesToMerge($Context);

if (!empty($Configuration['LOWCALVANILLA_PACKER_COMPRESSION_ENABLE'])) {
	$Response->Compress = true;
}

if (!$Files) {
	$Response->NotFound();
} else {
	if (!$Files->HasChanged($ETag, $MathTime)) {
		$Response->NotChanged();
	} else {
		$Content = $Files->GetContent();
		// Set headers
		$Response->AppendHeaders($Files->GetDetails());
		$Response->AddHeader('Cache-Control', 'private');
		$Response->AddHeader('Pragma', '');
		// Set body
		$Response->SetBody($Content);
		// Send Body and Headers
		$Response->SentResponse();
	}
}